<?php

/**
  +----------------------------------------------------------
 * 通过分类cid生成筛选条件html 
  +----------------------------------------------------------
 * @param  [type] $cid     [分类id]
 * @param  [array] $get [属性字符串]
 * @return [type]          [html]
   +----------------------------------------------------------
 */
//任务是否可以接
function checkCanPick($task, $mid)
	{
		if (empty($mid)) {
			$mid = $_SESSION['uid'];
		}

		if (empty($mid)) {
			return array('status'=>'-1','num'=>'1','message'=>'请先登录');
			
		}

	/*	if (substr($task['type'], 0, 7) == 'pyramid') {
			if (!p('commission')->isAgent($mid)) {
				return error(1, '只有分销商能接此任务');
			}
		}
		*/

		$time = time();

		if ($time < $task['starttime']) {
			return array('status'=>'-1','num'=>'-1','message'=>'任务尚未开放');
		}

		if ($time < $task['endtime']) {
			return array('status'=>'-1','num'=>'-1','message'=>'任务已结束');
		}

		$sql = 'select * from ' . tablename('jy_renwu_task_record') . ' where mid = :mid and taskid = :taskid and uniacid = :uniacid order by id desc';
		$lastRecord = pdo_fetch($sql, array(':mid' => $mid, ':taskid' => $task['id'], ':uniacid' => $_SESSION['uniacid']));

		if (empty($lastRecord)) {
			
			return array('status'=>'1','num'=>'','message'=>'');
		}

		if ($task['repeat_type'] == 1) {
			return array('status'=>'-1','num'=>'-1','message'=>'任务已结束');
			
		}

		$finishtime = strtotime($lastRecord['finishtime']);
		$stoptime = strtotime($lastRecord['stoptime']);
		if ($finishtime < 0 && ($time < $stoptime || $stoptime < 0)) {
		/*	if ($_W['ispost'] && !empty($_GPC['mid'])) {
				$this->send2wechat($lastRecord['id'], $_GPC['mid']);
			}
		*/
			return array('status'=>'-1','num'=>'-1','message'=>'任务未完成，不能重复领取');
		}

		if (0 < $finishtime) {
			$compareTime = $finishtime;
		}
		else {
			if ($time < $stoptime) {
				$compareTime = $stoptime;
			}
		}

		if ($task['repeat_type'] == 0) {
			return array('status'=>'1','num'=>'','message'=>'');
		}

		if ($task['repeat_type'] == 2) {
			if ($task['repeat_interval'] < $time - $compareTime) {
				return array('status'=>'1','num'=>'','message'=>'');
			}

		
			return array('status'=>'-1','num'=>'-1','message'=>$task['repeat_interval'] . '秒后才能再接此任务');
		}

		if ($task['repeat_type'] == 3) {
			if ($task['repeat_cycle'] == 1) {
				if (86400 < (int) strtotime(date('Ymd')) - (int) strtotime(date('Ymd', $compareTime))) {
					return array('status'=>'1','num'=>'','message'=>'');
				}
				return array('status'=>'-1','num'=>'-1','message'=>'明天才能再接此任务');
			}

			if ($task['repeat_cycle'] == 2) {
				$w = date('w', $compareTime);
				$w == 0 && ($w = 7);
				$between = diffBetweenTwoDays($compareTime, $time);

				if (7 < $w + $between) {
					return array('status'=>'1','num'=>'','message'=>'');
				}

				return array('status'=>'-1','num'=>'-1','message'=>'下个周才能再接此任务');
			}

			if ($task['repeat_cycle'] == 3) {
				if (0 < date('Ym') - date('Ym', $compareTime)) {
					return array('status'=>'1','num'=>'','message'=>'');
				}

				
				return array('status'=>'-1','num'=>'-1','message'=>'下个月才可以再接此任务');
			}
		}
		else {
			
			return array('status'=>'-1','num'=>'-1','message'=>'重复领取类型不详');
		}

		return array('status'=>'1','num'=>'','message'=>'');
	}
/**
     * 两天相差的天数
     */
 function diffBetweenTwoDays($day1, $day2)
{
	$second1 = strtotime(date('Y-m-d', $day1));
	$second2 = strtotime(date('Y-m-d', $day2));

	if ($second1 < $second2) {
		$tmp = $second2;
		$second2 = $second1;
		$second1 = $tmp;
	}

	return ($second1 - $second2) / 86400;
}
function stoptime($task)
{
	$time = time();
	$stoptime = '0000-00-00 00:00:00';
	//print_r($task['stop_type'].'gggg');exit;
	if ($task['picktype'] == 1) {
		return $task['endtime'];
	}

	if ($task['stop_type'] == 1) {
		$stoptime = date('Y-m-d H:i:s', $time + $task['stop_limit']);
	}
	else if ($task['stop_type'] == 2) {
		$stoptime = $task['stop_time'];
	}
	else {
		if ($task['stop_type'] == 3) {
			switch ($task['stop_cycle']) {
			case '0':
				$stoptime = date('Y-m-d 00:00:00', strtotime('+1 day'));
				break;

			case '1':
				$stoptime = date('Y-m-d 00:00:00', strtotime('next Monday'));
				break;

			case '2':
				$stoptime = date('Y-m-d 00:00:00', mktime(0, 0, 0, date('n') + 1, 1, date('Y')));
				break;
			}
	  	}
	}

	return $stoptime;
}
 /* task插件公用save方法,添加/编辑任务表
     * @param $table
     * @param $task
     * @param bool $Update 是否支持更新
     * @return bool
     */
function taskSave($table, $task, $Update = true)
{
	//checkDbFormat($table, $task);
	
	/*if (!is_array($task)) {
		return false;
	}
  */
	$task['uniacid'] = intval($_SESSION['uniacid']);
	//$isIdKey = in_array('id',$table);
	$isIdKey = true;
	if (empty($task['id']) && $isIdKey) {
		pdo_insert($table, $task);
		return pdo_insertid();
	}

	if (!$isIdKey) {
		$countSql = 'select count(*) from ' . tablename($table) . ' where uniacid = :uniacid';
		$ifExist = pdo_fetchcolumn($countSql, array('uniacid' => $_SESSION['uniacid']));
		if ($ifExist && $Update) {
			pdo_update($table, $task, array('uniacid' => $_SESSION['uniacid']));

			if (empty($task['id'])) {
				return $_SESSION['uniacid'];
			}

			return $task['id'];
		}

		if (!$ifExist) {
			pdo_insert($table, $task);
			return pdo_insertid();
		}

		return false;
		}

		if ($Update) {
			pdo_update($table, $task, array('id' => $task['id']));

			if (empty($task['id'])) {
				return $_SESSION['uniacid'];
			}

			return $task['id'];
		}

		return false;
	}
/**
     * 校验数据库字段
     * @param $task
     */
function checkDbFormat($table, &$task)
{   
	if (!is_array($task) || !is_array($table)) {
		return $task = false;
	}
	$field = array_flip($table);
	$diff = array_diff_key($task, $field);

	if (!empty($diff)) {
		return $task = false;
	}

	foreach ($task as &$t) {
		if (is_array($t)) {
			$t = json_encode($t);
		}
	}

	return true;
}
function getTaskType($type = '')
{  
	$task_type= pdo_getall('jy_renwu_task_type');
	if (empty($type)) {
		
		return $task_type;
	}
	
	foreach ($task_type as $tasktype) {
		if ($tasktype['type_key'] == $type) {
			return $tasktype;
		}
	}

	return false;
}
//检查任务是否完成
function checkTask($task_id){
	
}
?>
